package com.bjb.vr.remote.ws.rabbitmq.handler.impl;

import com.alibaba.fastjson.JSONObject;
import com.bjb.vr.common.dto.ResGroup;
import com.bjb.vr.common.service.ResContentService;
import com.bjb.vr.common.service.ResDeviceService;
import com.bjb.vr.common.service.ResGroupService;
import com.bjb.vr.common.service.ResPlatformResourceService;
import com.bjb.vr.common.utils.RedisUtil;
import com.bjb.vr.remote.ws.rabbitmq.constants.MessageActionConstants;
import com.bjb.vr.remote.ws.rabbitmq.entity.RabbitWebSocketMsg;
import com.bjb.vr.remote.ws.rabbitmq.handler.MessageHandler;
import com.bjb.vr.remote.ws.redis.constants.RedisConstants;
import com.bjb.vr.remote.ws.websocket.vo.Message;
import com.bjb.vr.remote.ws.websocket.websocket.WebSocketMsgHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @version 1.0.0
 * @description:
 * @author: caihaibo
 * @createDate: 2022/3/30
 */
@Slf4j
@Component
public class StartClassMessageHandler implements MessageHandler {

    @Autowired
    ResDeviceService resDeviceService;

    @Autowired
    ResGroupService resGroupService;

    @Autowired
    ResContentService resContentService;

    @Autowired
    ResPlatformResourceService resPlatformResourceService;

    @Autowired
    RedisUtil redisUtil;

    final String  LOCK_KEY = "res_group_class_state_start";

    @Override
    public void process(RabbitWebSocketMsg rabbitWebSocketMsg) {

        String dbLockKey = RedisConstants.WEBSOCKET_DB_OP_LOCK_KEY + LOCK_KEY+ rabbitWebSocketMsg.getGroupCode();

        JSONObject jsonObject = JSONObject.parseObject(rabbitWebSocketMsg.getMsg());

        Message message = JSONObject.toJavaObject(jsonObject, Message.class);

        if(message.getAction().equals(MessageActionConstants.MESSAGE_ACTION_START_CALSS)){
            log.info("处理上课消息");
            JSONObject _jsonObject = JSONObject.parseObject(message.getCommand().getParameter());
            String groupCode = rabbitWebSocketMsg.getGroupCode();
            long contentId = _jsonObject.getLong("id");
            if(contentId!=0) {
                WebSocketMsgHandler.sendAllClient(rabbitWebSocketMsg);
                String value = String.valueOf(System.currentTimeMillis());
                if(redisUtil.lock(dbLockKey,value,RedisConstants.WEBSOCKET_DB_OP_LOCK_KEY_EXPIRE_TIME)) {

                    resDeviceService.initDeviceClassLoadStateByGroupCode(groupCode, -1, -1, "");
                    ResGroup resGroup = new ResGroup();
                    resGroup.setGroupCode(rabbitWebSocketMsg.getGroupCode());
                    resGroup.setContentId(contentId);
                    resGroup.setClassState(1);
                    resGroup.setSectionNo(0);
                    resGroupService.updateResGroupByGroupCode(resGroup);
                    resContentService.updateResContentUseCount(contentId);
                    String tenantId = rabbitWebSocketMsg.getTenantId();
                    if(!StringUtils.isEmpty(tenantId)) {
                        resPlatformResourceService.updateUseCount(contentId,tenantId);
                    }
                    redisUtil.unlock(dbLockKey, value);
                }
            }
        }
    }
}
